package com.example.lovedogs.zizhen;

import java.lang.reflect.Type;
import java.util.List;

import org.json.JSONArray;

import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.example.lovedogs.R;
import com.example.lovedogs.R.id;
import com.example.lovedogs.R.layout;
import com.example.lovedogs.R.string;
import com.example.lovedogs.domain.SickInfoDisease;
import com.example.lovedogs.util.SpanUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SickInfoDiseaseActivity extends Activity {
	private LinearLayout sifdisease_llayout;
	private ListView sifdisease_lv;
	private MyAdapter adapter;
	private String key;
//	private String url="http://192.168.191.2:8080/LoveDogs04/JsonSickInfoDiseaseServlet";
	private String url;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sickinfodisease);
		titlebar();
		url=getString(R.string.serverurl)+"JsonSickInfoDiseaseServlet";
		key=getIntent().getStringExtra("keyword");
		init();
		lvadapter();
		sifdisease_lv.setOnItemClickListener(new MyOnItemClickListener());
		
	}
	private void titlebar() {
		ImageView imageView1=(ImageView) findViewById(R.id.imageView1);
		
		imageView1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
					finish();
			}
		});
		TextView textView1=(TextView) findViewById(R.id.textView1);
		textView1.setText("疾病列表");
	}

	private void lvadapter() {
		RequestParams params=new RequestParams();
		params.addBodyParameter("key",key);
		Toast.makeText(SickInfoDiseaseActivity.this, key, 0).show();
		
		HttpUtils http=new HttpUtils();
		http.send(
				HttpMethod.POST, 
				url, 
				params,
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						Gson g=new Gson();
						Type type=new TypeToken<List<SickInfoDisease>>(){}.getType();
						List<SickInfoDisease> data=g.fromJson(responseInfo.result, type);
						
						if (adapter==null) {
							adapter = new MyAdapter(data);
						}else{
							adapter.setData(data);
						}
						adapter.notifyDataSetChanged();
						sifdisease_lv.setAdapter(adapter);
						
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						Toast.makeText(SickInfoDiseaseActivity.this,
								"onErrorResponse" + error.getMessage(),
								Toast.LENGTH_LONG).show();
						
					}
				});
		
	}


	private void init() {
		sifdisease_llayout=(LinearLayout) findViewById(R.id.sifdisease_llayout);
		sifdisease_lv=(ListView) findViewById(R.id.sifdisease_lv);
		TextView sifdisease_tv1=new TextView(this);
		LinearLayout.LayoutParams tvparams=new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT, 
				LinearLayout.LayoutParams.WRAP_CONTENT);
		sifdisease_tv1.setLayoutParams(tvparams);
		sifdisease_tv1.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);
		sifdisease_tv1.setText(key+">");
	    sifdisease_llayout.addView(sifdisease_tv1);
		
	}
	private class MyAdapter extends BaseAdapter{
		private List<SickInfoDisease> data;
		private LayoutInflater inflater;

		public MyAdapter(List<SickInfoDisease> data) {
			this.data=data;
			inflater=LayoutInflater.from(SickInfoDiseaseActivity.this);
		}
		
		public List<SickInfoDisease> getData() {
			return data;
		}

		public void setData(List<SickInfoDisease> data) {
			this.data = data;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			SickInfoDisease info=data.get(position);
			if (convertView==null) {
				convertView=inflater.inflate(R.layout.activity_sickinfodisease_listitem, null);
			}
			TextView sifdisease_listitem_tv_name=(TextView) convertView.findViewById(R.id.sifdisease_listitem_tv_name);
			TextView sifdisease_listitem_tv_symptom=(TextView) convertView.findViewById(R.id.sifdisease_listitem_tv_symptom);
			sifdisease_listitem_tv_name.setText(info.getName());
			
			String symptom=info.getSymptom();
		/*	SpannableStringBuilder sp = new SpannableStringBuilder(symptom);
			int start=symptom.indexOf(key);
			int end=start+key.length();
			sp.setSpan(new ForegroundColorSpan(Color.RED),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);*/
			
			SpannableStringBuilder sp=SpanUtil.setColor(key,symptom,Color.RED);
			sifdisease_listitem_tv_symptom.setText(sp);
			return convertView;
		}
		
	} 
	public class MyOnItemClickListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			SickInfoDisease sdemo=(SickInfoDisease) adapter.getItem(position);
			int no=sdemo.getNo();
//			SpannableStringBuilder sp=adapter.getSp();
			Intent intent=new Intent(SickInfoDiseaseActivity.this,SickInfoDiseaseDetailsActivity.class);
			intent.putExtra("no", no);
//			intent.putExtra("sp", sp);
			intent.putExtra("key", key);
			startActivity(intent);
			
		}
		
	}
}
